package de.uni.kassel.campusappver3.crawler;

import java.util.regex.Matcher;

import de.uni.kassel.campusappver3.model.CrawlerReturnValue;

/**
 * returns meals and prices provided by MENSA
 * 
 * 
 * keys: DAYNAMES = {"Mo", "Di", "Mi", "Do", "Fr"};
 * values: list with mealname and price
 * @author ckonze
 *
 */
public class MensaCrawler extends Crawler {
	public final static String[] DAYNAMES = {"Mo", "Di", "Mi", "Do", "Fr"};

	public MensaCrawler(ICrawl listener) {
		super(listener);
	}

	@Override
	protected CrawlerReturnValue backgoundCrawl(Object[] params,
			CrawlerReturnValue crv) {
		try {
			// get meals
			Matcher mat = getUrlMatcher(
					"http://www.studentenwerk-kassel.de/?id=189",
					"valign=\"top\" width=\"150px\" height=\"40\">(.*?)</td>");
			// get prices
						Matcher prices = getUrlMatcher(
								"http://www.studentenwerk-kassel.de/?id=189",
								"<td class=\"preis(.*?)>(.*?)</td>");

			int i = 0;
			int iprice = 0;
			while (prices.find()) {				
				if(mat.find()){					
					String match = removeTagsEntitys(mat.group(0));
					match = match.replaceAll("v(.*?)>", "");
					crv.appendValue(DAYNAMES[i % 5], match);
					i++;
				}
				if (!prices.group(0).contains("Preis")) {
					String match2 = removeTagsEntitys(prices.group(0)).replace(" ", "");
					match2 = match2.replaceAll(".$", "") + ((char) 8364);
					crv.appendValue(DAYNAMES[iprice % 5], match2);
					iprice++;
				}
				
			}
			
			
		} catch (Exception e) {
			System.out.println("Error in AsyncTask:" + e.getStackTrace());
			e.printStackTrace();
		}		
		return crv;

	}

	
	

}
